<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>公共字段</title>
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport"
		  content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<meta http-equiv="Cache-Control" content="no-siteapp"/>
	<script src="/style/public/publicStyle.js"></script>
</head>

<body>

<!--表格-->
<div class="weadmin-body">
	<div class="layui-row">
		<table class="layui-hide" id="tableDataList" lay-filter="usertab"></table>
	</div>
</div>

<!--表格操作-->
<script type="text/html" id="op">
	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改表</a>
</script>

<div class="layui-row" id="tableForm" style="display:none;">
	<div class="layui-col-md11">
		<form class="layui-form layui-from-pane" id="formId" style="margin-top:20px" lay-filter="tableForm">
			<input type="hidden" name="idDim"/>
			<div class="layui-row">
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">维度</label>
						<div class="layui-input-block">
							<input type="text" name="idPubfld" disabled="disabled" class="layui-input">
						</div>
					</div>
				</div>
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">维度表</label>
						<div class="layui-input-block">
							<input type="text" name="cdTbDim" id="cdTbDim" disabled="disabled" class="layui-input">
						</div>
					</div>
				</div>
			</div>
			<div class="layui-row">
				<div class="layui-col-md12">
					<div class="layui-form-item">
						<div class="layui-input-block" style="float: right;">
							<button class="layui-btn layui-btn-normal" type="button" id="tableDDLSave">保存</button>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-row">
				<div class="layui-col-md12">
					<div class="layui-form-item">
						<table class="layui-hide" id="tableColumnList"
							   lay-filter="tableColumnList"></table>
					</div>
				</div>
				<div class="layui-col-md12">
					<div class="layui-form-item" style="float: right;">
						<button type="button" class="layui-btn  layui-btn-normal" id="addColumnBtn">
							添加列
						</button>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>

<div class="layui-row" id="addColumn" style="display:none;">
	<div class="layui-col-md11">
		<form class="layui-form layui-from-pane" id="addColumnForm" style="margin-top:20px" lay-filter="addColumnForm">
			<div class="layui-row">
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">列名</label>
						<div class="layui-input-block">
							<input type="text" class="layui-input" id="id_pubfld">
						</div>
					</div>
				</div>
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">列类型</label>
						<div class="layui-input-block">
							<select name="tbTar" lay-filter="mainSelectIdFilter" lay-verify="required"
									lay-search="" id="mainSelectId">
								<option value="">直接选择或搜索选择</option>
							</select>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-row">
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">长度</label>
						<div class="layui-input-block">
							<input type="text" class="layui-input" id="num_lth">
						</div>
					</div>
				</div>
				<div class="layui-col-md6">
					<div class="layui-form-item">
						<label class="layui-form-label">精度</label>
						<div class="layui-input-block">
							<input type="text" class="layui-input" id="num_prec">
						</div>
					</div>
				</div>
			</div>
			<div class="layui-row">
				<div class="layui-col-md12">
					<div class="layui-form-item">
						<div class="layui-input-block" style="float: right;">
							<button class="layui-btn" id="addSave" type="button">添加</button>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>
</body>
<script>
	layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element'], function () {
		var currPage;
		var util = layui.util;
		var laydate = layui.laydate //日期
				, laypage = layui.laypage //分页
				, layer = layui.layer //弹层
				, table = layui.table //表格
				, carousel = layui.carousel //轮播
				, upload = layui.upload //上传
				, element = layui.element //元素操作
				, form = layui.form;
		var logintoken = null;
		getSharreTieba_LoginToken();
		table.render({
			elem: '#tableDataList'
			, even: true //开启隔行背景
			, cellMinWidth: 80
			, toolbar: ''
			, url: global_visit_url_prefix + '/etl/mdMedPubfld/findIsDimPubfld'
			, where: {
				LoginKey: logintoken,
			}
			, cols: [[
				, {field: 'idTb', title: 'idTb', hide: true}
				, {field: 'idPubfld', title: 'idPubfld', hide: true}
				, {field: 'idPubfld', title: '维度'}
				, {field: 'na', title: '维度名称'}
				, {field: 'cdDim', title: '维度简称'}
				, {field: 'cdTbDim', title: '维度表'}
				, {fixed: 'right', title: '操作', align: 'center', toolbar: '#op'}
			]]
			, page: true
			, limit: 10
			, done: function (rest, curr, count) {
				currPage = curr;
			}
		});

		table.render({
			elem: '#tableColumnList'
			, even: false //开启隔行背景
			, id: 'tableColumnList'
			, url: global_visit_url_prefix + '/etl/mdMedPubfld/findTableFld'
			, where: {'table': ''}
			, cols: [[
				, {field: 'idPubfld', title: '字段'}
				, {field: 'naPubfld', title: '名称'}
				, {field: 'euJavatp', title: '类型1', hide: true}
				, {field: 'naJavatp', title: '类型'}
				, {field: 'numLth', title: '长度', edit: 'text'}
				, {field: 'numPrec', title: '精度', edit: 'text'}
				, {fixed: 'right', title: '操作', align: 'center', toolbar: '#op2'}
			]]
		});

		var index;
		$("#addColumnBtn").on('click', function () {
			index = layer.open({
				//layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
				type: 1,
				title: "增加列",
				area: ['750px', '250px'],
				content: $("#addColumn")//引用的弹出层的页面层的方式加载修改界面表单
			});
			$("#id_pubfld").val("");
			$("#mainSelectId").val("");
			$("#num_lth").val("0");
			$("#num_prec").val("0");
			$(".layui-select-tips").click();
			$(".layui-this").removeClass("layui-this");
		})


		$("#tableDDLSave").on('click', function () {
			var cacheElement = table.cache["tableColumnList"];

			$.ajax({
				url: '/etl/mdMedPubfld/saveDimTableFld',
				type: 'post',
				dataType: 'json',
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(cacheElement),
				success: function (msg) {
					if (msg.code == 1) {
						alert(msg.msg);
					} else {
						table.reload("tableDataList", {page: {curr: currPage}});
						layer.close(indexA);

					}
				}
			})
		});

		$("#addSave").on('click', function () {
			var cacheElement = table.cache["tableColumnList"];
			var param = {};
			param.idPubfld = $("#id_pubfld").val();
			if (param.idPubfld == undefined || param.idPubfld == 'undefined' || param.idPubfld == '' || param.idPubfld.trim() == '') {
				alert("列名不能为空")
				return
			}

			for (var i = 0; i < cacheElement.length; i++) {
				if (cacheElement[i].idPubfld.toUpperCase() == param.idPubfld.toUpperCase()) {
					alert("列已存在");
					return;
				}
			}


			param.naPubfld = $("#id_pubfld").val();
			param.euJavatp = $("#mainSelectId").val();
			if (param.euJavatp == undefined || param.euJavatp == 'undefined' || param.euJavatp == '' || param.euJavatp.trim() == '') {
				alert("类型不能为空")
				return
			}
			param.naJavatp = $("#mainSelectId option:selected").text();
			param.numLth = $("#num_lth").val();
			if (param.numLth == undefined || param.numLth == 'undefined' || param.numLth == '' || param.numLth.trim() == '') {
				alert("长度不能为空")
				return
			}
			param.numPrec = $("#num_prec").val();
			if (param.numPrec == undefined || param.numPrec == 'undefined' || param.numPrec == '' || param.numPrec.trim() == '') {
				alert("精度不能为空")
				return
			}
			cacheElement.push(param);
			layui.table.reload("tableColumnList", {url: '', data: cacheElement});
			layer.close(index);
		})

		$.ajax({
			url: '/etl/baseDbTpcomp/findJavaType',
			type: 'get',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			success: function (data1) {
				var data = data1.data; //提出返回数据中的data
				form.val("tableForm", data);
				if (data !== null) {
					$.each(data, function (index, item) { //循环加入 data的元素
						$("#mainSelectId").append("<option value='" + item.cd + "'>" + item.na + "</option>");
					});
					form.render('select');
				}
			}
		});

//监听行工具事件
		var indexA;
		table.on('tool(usertab)', function (obj) { //注：tool 是工具条事件名，usertab 是 table 原始容器的属性 lay-filter="对应的值"
			var data = obj.data //获得当前行数据
					, layEvent = obj.event; //获得 lay-event 对应的值
			if (layEvent == 'edit') {
				indexA = layer.open({
					//layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
					type: 1,
					title: "编辑维度表",
					area: ['750px', '600px'],
					content: $("#tableForm")//引用的弹出层的页面层的方式加载修改界面表单
				});
				//动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
				setFormValue(obj, data);
				layui.table.reload("tableColumnList", {
					url: global_visit_url_prefix + '/etl/mdMedPubfld/findTableFld',
					where: {'table': data.cdTbDim}
				});
			}
		});

		table.on('tool(tableColumnList)', function (obj) {
			var data = obj.data
					, layEvent = obj.event;
			if (layEvent == 'deleteColumn') {
				var idPubfld = data.idPubfld;
				if (idPubfld == 'cd' || idPubfld == 'na' || idPubfld == 'des') {
					alert("默认列不能删除");
					return
				}

				var cacheElement = table.cache["tableColumnList"];
				for (var i = 0; i < cacheElement.length; i++) {
					if (cacheElement[i].idPubfld == idPubfld) {
						cacheElement.splice(i, 1);
						layui.table.reload("tableColumnList", {url: '', data: cacheElement});
					}
				}
			}
		});

		$("body").on("keypress", "#hiddenFilter", function (event) {
			var content = $(this).val();
			if (event.keyCode == '13') {
				//执行重载
				table.reload('tableDataList', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
					, where: {
						filter: content		//在表格中进行搜索
					}
				});
				$("#hiddenFilter").val(content);
			}
		});

//监听表头
		table.on('toolbar(usertab)', function (obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			switch (obj.event) {
				case 'getCheckData':
					layer.open({
						//layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
						type: 1,
						title: "新增数据源",
						area: ['520px', '530px'],
						content: $("#tableForm")//引用的弹出层的页面层的方式加载修改界面表单
					});
					setFormValue(obj, null);
					break;
			}
			;
		});

		//监听弹出框表单提交，massage是修改界面的表单数据'submit(demo11),是修改按钮的绑定
		function setFormValue(obj, data) {
			if (data == null) {
				$("#formId")[0].reset();
			} else {
				form.val("tableForm", data);
			}
			form.on('submit(demo11)', function (massage) {
				$.ajax({
					url: '/etl/mdMedDim/save',
					type: 'post',
					dataType: 'json',
					contentType: 'application/json;charset=UTF-8',
					data: JSON.stringify(massage.field),
					success: function (msg) {

						table.reload("tableDataList");
					}
				})
			})
		}
	});


</script>

</html>